﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace OpenApi
{
	/// <summary>
	/// 服务信息。
	/// </summary>
	public class ServiceInfo
	{
		/// <summary>
		/// 
		/// </summary>
		public ServiceInfo() { }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="serviceContract"></param>
		/// <param name="serviceContractImpl"></param>
		/// <param name="serviceName"></param>
		public ServiceInfo(Type serviceContract, Type serviceContractImpl, string serviceName)
		{
			this.Contract = serviceContract;
			this.ContractImpl = serviceContractImpl;
			this.Name = this.GetSvcName(serviceName);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="serviceContract"></param>
		/// <param name="serviceContractImpl"></param>
		public ServiceInfo(Type serviceContract, Type serviceContractImpl)
			: this(serviceContract, serviceContractImpl, null)
		{
		}

		/// <summary>
		/// 服务协定。
		/// </summary>
		public Type Contract { get; set; }

		/// <summary>
		/// 服务协定实现。
		/// </summary>
		public Type ContractImpl { get; set; }

		/// <summary>
		/// 服务名称。
		/// </summary>
		public string Name { get; set; }

		string GetSvcName(string serviceName)
		{
			if (!String.IsNullOrWhiteSpace(serviceName)) return serviceName;

			var attr = (ServiceContractAttribute)this.Contract.GetCustomAttributes(typeof(ServiceContractAttribute), false)[0];
			if (String.IsNullOrWhiteSpace(attr.Name))
			{
				return Contract.Name;
			}
			else
			{
				return attr.Name;
			}
		}
	}
}
